package com.ruoyi.web.controller.workflow;

import java.io.IOException;
import java.io.InputStream;
import java.rmi.ServerException;
import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import com.ruoyi.common.utils.file.FileUtils;
import org.apache.commons.io.IOUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.workflow.domain.BpmnInfo;
import com.ruoyi.workflow.service.IBpmnInfoService;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 流程定义Controller
 *
 * @author lhl
 * @date 2022-11-14
 */
@RestController
@RequestMapping("/workflow/bpmninfo")

public class BpmnInfoController extends BaseController
{
    @Autowired
    private IBpmnInfoService bpmnInfoService;
    private final List<String> SUPPORT_FILETYPE= Arrays.asList("bpmn","xml","zip");

    /**
     * 查询流程定义列表
     */
    @PreAuthorize("@ss.hasPermi('workflow:bpmninfo:list')")
    @GetMapping("/list")
    public TableDataInfo list(BpmnInfo bpmnInfo)
    {
        startPage();
        List<BpmnInfo> list = bpmnInfoService.selectBpmnInfoList(bpmnInfo);
        return getDataTable(list);
    }

    /**
     * 删除流程定义
     */
    @PreAuthorize("@ss.hasPermi('workflow:bpmninfo:remove')")
    @Log(title = "流程定义", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        if(ids == null || ids.length == 0) {
        return error("请选择要删除的流程定义");
    }
        return toAjax(bpmnInfoService.deleteBpmnInfoByIds(ids));
    }

    @PreAuthorize("@ss.hasPermi('workflow:bpmninfo:deploy')")
    @Log(title = "流程部署", businessType = BusinessType.INSERT)
    //文件上传只能使用post请求
    @PostMapping("deploy")
    public AjaxResult deploy(MultipartFile file,BpmnInfo bpmnInfo) throws IOException {
        //文件类型校验
        String filename = file.getOriginalFilename();
        String suffix = FileUtils.getSuffix(filename);
        if (!SUPPORT_FILETYPE.contains(suffix)){
             return AjaxResult.error(400,"暂只支持"+SUPPORT_FILETYPE+"类型的文件");
        }
        bpmnInfo.setFileName(filename);
        bpmnInfoService.deploy(file.getInputStream(),bpmnInfo);
        return AjaxResult.success("部署成功");
    }
    @PreAuthorize("@ss.hasPermi('workflow:bpmninfo:view')")
    @GetMapping("/view/{id}")
    public void view(@PathVariable Long id,String type,HttpServletResponse response) throws IOException {
        if ("image".equals(type)||"bpmn".equals(type)){
            InputStream view= bpmnInfoService.getView(id,type);
            IOUtils.copy(view,response.getOutputStream());
        }else
        {
            throw new ServerException("文件类型不匹配");
        }
    }
}
